import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';

export class Http {
  http: AxiosInstance;
  constructor(url: string) {
    this.http = axios.create({
      baseURL: url,
    });
    this.interceptors();
  }

  /**
   * 对请求和响应拦截操作
   */
  interceptors(): void {
    this.http.interceptors.request.use(function (
      reqConfig: AxiosRequestConfig,
    ): Promise<AxiosRequestConfig> {
      // todo
      return Promise.resolve(reqConfig);
    });
    this.http.interceptors.response.use(
      function (res: AxiosResponse): AxiosResponse {
        // todo
        return res;
      },
      function (error) {
        if (error.response.status !== 200) {
          return Promise.reject(error);
        }
      },
    );
  }
}
